# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver

from webdriver_base import WebdriverBase
from common.my_file import MyFile

'''
上交所监管数据
'''

'''
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path="D:\\chromedriver.exe")

file = open('./out/shangjiaosuo_zhishu_list_data.txt', 'w', encoding = 'utf-8')
file_name = open('./out/shangjiaosuo_zhishu_list_name.txt', 'w', encoding = 'utf-8')
'''


class ShZhishuListData(WebdriverBase):
    def __init__(self):
        self.db = MysqlHander("config.ini")
        self.file = open('./out/shangjiaosuo_zhishu_list_data.txt', 'w', encoding = 'utf-8')
        self.file_name = open('./out/shangjiaosuo_zhishu_list_name.txt', 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

    def add_simple_task(self, task_info, task_type = 1001):
        sql = "insert into simple_task (Ftask_info, Ftask_type, Fcreate_time, Fmodify_time) values "
        sql += "('" + task_info + "'," + str(task_type) + ", now(), now());"
        print(sql)
        try:
            self.db.execute_not_safe(sql)
        except Exception as e:
            pass

    def add_zhishu(self, business, attr_id = 2001):
        sql = "insert into attribute_data (Fattribute_name, Frelation_id, Fmatch_text, Fcreate_time, Fmodify_time) values "
        sql += "('" + business + "'," + str(attr_id) + ", '', now(), now());"
        print(sql)
        try:
            self.db.execute_not_safe(sql)
        except Exception as e:
            print(e)
            pass

    def query_page_data(self, page):
        time.sleep(10)
        tbodys = self.driver.find_elements_by_tag_name("tbody")
        for tbody in tbodys:
            trs = tbody.find_elements_by_tag_name("tr")
            for tr in trs:
                tds = tr.find_elements_by_tag_name("td")
                if len(tds) < 3:
                    print("continue")
                    continue
                #print(tds)
                if len(tds[0].text) < 3:
                    continue
                a_tag = tds[0].find_element_by_tag_name("a")
                #print(a_tag)
                if a_tag:
                    attribute_url = tds[0].find_element_by_tag_name("a").get_attribute("href")
                    attribute_name = tds[0].find_element_by_tag_name("a").text
                    attribute_all = "SH|" + tds[1].text + "|" + attribute_name + "|" + attribute_url + "|"
                    
                    self.add_simple_task("SH" + tds[1].text + "," + attribute_all)
                    self.file.write("SH" + tds[1].text + "," + attribute_all + "\n")
                    
                    self.add_zhishu(attribute_name)
                    self.file_name.write(attribute_name + "\n")
        
        return False
        
    def query_data(self):
        self.driver.get("http://www.sse.com.cn/market/sseindex/indexlist/")
        page = 1
        while self.query_page_data(page):
            print(page)
            page = page + 1
            time.sleep(3)
        self.driver.quit()
    
    def get_data(self):
        self.query_data()

if __name__ == '__main__':
    d = ShZhishuListData()
    d.get_data()

